package descentFly.app.dialogs.levelsWindow
{
	import SEApp.se_util;
	
	import UIFree.ObjectEvent;
	import UIFree.controls.uif_checkbox;
	import UIFree.controls.uif_gbutton;
	import UIFree.controls.uif_inputLayer;
	import UIFree.controls.uif_label;
	
	import UIUtils.uif_util;
	
	import descentFly.app.df_block;
	import descentFly.app.df_events;
	import descentFly.app.df_label;
	import descentFly.app.df_modalLayer;
	import descentFly.app.df_window;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class df_battleParams extends df_window {
		public static const INPUTLABELWIDTH: int = 120;
		public static var SIZE: Point = new Point(500, 280);
		private var _battle		: Object;
		private var _name		: uif_inputLayer;
		private var _levelID	: uif_inputLayer;
		private var _maxUsers	: uif_inputLayer;
		private var _rank		: uif_inputLayer;
		private var _monsters	: uif_checkbox;
		
		public function df_battleParams(a_parent:df_modalLayer, a_battle: Object) {
			_battle 	= a_battle;
			super(a_parent, SIZE.x, SIZE.y, 'window', true, 1);
			title.text 	= locale.BATTLEPARAMS;
		}
		
		override protected function createDialogComponents(a_layer:df_block):void {
			var inner: Rectangle = a_layer.getInner();
			(new uif_label(a_layer, inner.x, inner.y, inner.width, 40, 'description')).text = locale.BATTELPARAMSDESCR;
			inner.top += 40;
			_name = new uif_inputLayer(a_layer, inner.x, inner.y, inner.width, INPUTHEIGHT, INPUTLABELWIDTH, locale.NAMEBATTLETITLE, _battle.name, 'inputLayer');
			inner.top += INPUTHEIGHT;
			_levelID = new uif_inputLayer(a_layer, inner.x, inner.y, INPUTLABELWIDTH + 40, INPUTHEIGHT, INPUTLABELWIDTH, 
				locale.LEVEL, (_battle.id?_battle.id:Space.scene.id).toString(), 'inputLayer');
			_levelID.input.textField.type = 'dynamic';
			var button: uif_gbutton = (new uif_gbutton(a_layer, inner.x + _levelID.width, inner.y, se_util.DEFWIDTHBUTTON, se_util.DEFHEIGHTBUTTON, 'button'));
			button.text = locale.SELECT;
			button.addEvent(MouseEvent.CLICK, doSelectLevel);
			
			inner.top += INPUTHEIGHT;
			_maxUsers = new uif_inputLayer(a_layer, inner.x, inner.y, inner.width, INPUTHEIGHT, INPUTLABELWIDTH, locale.MAXUSERSPERBATTLE, _battle.maxUsers, 'inputLayer');
			inner.top += INPUTHEIGHT;
			_monsters = new uif_checkbox(a_layer, inner.x, inner.y, inner.width, INPUTHEIGHT, 'checkbox');
			_monsters.text = locale.MONSTERSENABLE;
		}
		
		protected function doSelectLevel(e: Event): void {
			var window: df_levelsWindow = new df_levelsWindow(parentLayer, function(level: Object): void {
				_levelID.value 	= level.id;
				_battle.id		= level.id;
			}, 2);
			window.show();
		}
		
		override protected function createButtons(a_layer:df_block):void {
			addButton(locale.OK, doOkButton);
			addButton(locale.CANCEL, doButtonClose);
		}
		
		protected function getBattleObject(): Object {
			return uif_util.union(_battle, {
				name			: _name.value,
				id				: parseInt(_levelID.value),
				maxUsers		: _maxUsers.value,
				monstersIndex	: _monsters.checked?1:0
			});
		}
		
		protected function checkInput(): Boolean {
			return (_name.value != '') && (parseInt(_maxUsers.value) > 1);
		}
		
		protected function doOkButton(e: Event): void {
			if (checkInput()) {
				app.stage.dispatchEvent(new ObjectEvent(df_events.CREATEBATTLE, getBattleObject()));
				close();
			} else dispatchEvent(new ObjectEvent(df_events.ALERT, locale.ERRORBATTLEPARAMS, true));
		}
	}
}